#include "common.h"


using namespace Scenic::Renderer;


extern ResourcePool imagePool;

ResourcePool contextPool;


Context::Context()
: Resource(&contextPool)
{
	lineEdgeTexture = 0;
	lineDashTexture = 0;
	aaPolygonTexture = 0;
	polygonAntialias = false;
	oldClips[0] = 0;
	primitives = 0;
	clip = new ClipArea();
	clip->parent = 0;	
}

Context::~Context()
{
	freeDeviceResources();
	delete device;
}

bool Context::init()
{
#ifdef DIRECTX
	device = new Device(hwnd);
#endif
#ifdef OPT_X11
	device = new Device(window);
#endif

	return true;
}

void Context::reset()
{
	freeDeviceResources();
	device->reset();
}

void Context::freeDeviceResources()
{
	for(int i = 0; i < 1; i++) 
	{
		if(oldClips[i])
		{
			if(oldClips[i]->texture)
				delete oldClips[i]->texture;
			delete oldClips[i];
			oldClips[i] = 0;
		}
	}
}

void Context::setPrimitives(Primitives *p)
{
	if(primitives)
	{
		primitives->draw();
	}
	primitives = p;
}

Context *Scenic::Renderer::getContext(int id)
{
	return (Context*)contextPool.get(id);
}

void Context::setRenderTarget(Surface *target)
{
	SurfaceInfo info = target->getInfo();

	device->setRenderTarget(target);
	initializeViewport(info.width, info.height);
}

void Context::initializeViewport(int width, int height)
{
	device->initializeViewport(width, height);
}
